<template>
  <div class="register-style">
    <el-form
      ref="registerFormRef"
      :model="registerForm"
      :rules="rules"
      label-width="120px"
      status-icon
      class="register-form"
      @keyup.enter.native="onregisterBtn()"
    >
      <h1 class="title">注册</h1>
      <el-form-item label="账号" prop="username">
        <el-input v-model="registerForm.username" placeholder="账号"></el-input>
        <div>默认密码：123456</div>
      </el-form-item>
      <el-form-item label="姓名/昵称" prop="name">
        <el-input v-model="registerForm.name" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item label="手机" prop="phone">
        <el-input v-model="registerForm.phone" placeholder="手机"></el-input>
      </el-form-item>
      <div class="register-btn">
        <el-button type="primary" @click="onregisterBtn()">注册</el-button>
     
      </div>
    </el-form>
  </div>
</template>

<script>
// import { routes } from '@/router/routes';
import { getDataInfo, savaDataInfo } from '@/api/user';
export default {
  name: 'registerPage',
  data() {
    return {
      registerForm: {
        username: '',
        password: '',
      },
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' },
          { min: 3, max: 15, message: '用户名长度为3-15' },
        ],
        name: [
          { required: true, message: '姓名/昵称不能为空', trigger: 'blur' },
          { min: 3, max: 15, message: '用户名长度为3-15' },
        ],
        phone: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { min: 3, max: 15, message: '用户名长度为3-15' },
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 3, max: 15, message: '密码长度为3-15' },
        ],
      },
    };
  },
  created() {},
  methods: {
    onregisterBtn() {
      // sessionStorage.setItem('userInfo', JSON.stringify(this.registerForm));
      // this.$router.push({ path: '/' }).catch((err) => {
      //         console.log(err);
      //       });
      this.$refs['registerFormRef'].validate((valid) => {
        if (valid) {
             this.registerForm.password = '123456'
             this.registerForm.role = '2'
            savaDataInfo(this.registerForm)
              .then((data) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '注册成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false;
                      // this.$emit('refreshDataList');
                      this.$router.push('/login')
                    },
                  });
                }
              })
              .catch((err) => {
                console.log(err);
              });
          }
      });

    },
    onResetBtn() {
      this.$refs.resetPsdRef.resetFields();
    },
  },
};
</script>

<style scoped>
.register-style {
  width: 100vw;
  height: 100vh;
  background-image: url('../../assets/login.png');
  background-size: 100% 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.register-form {
  width: 500px;
  background-color: #fff;
  padding: 20px;
  padding-top: 0;
}

.title {
  text-align: center;
  margin: 20px 0;
}

.register-btn {
  display: flex;
  justify-content: center;
}
</style>
